package com.SMP.controller.class_room;

import com.SMP.model.*;
import com.SMP.service.BroadcastService;
import com.SMP.service.class_room.CourseService;
import com.SMP.service.class_room.EquipmentService;
import com.SMP.service.class_room.RoomService;
import com.SMP.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zlz
//	 * @param 设备
 * */

@Controller
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    @Autowired
    CourseService courseService;

    @Autowired
    RoomService roomService;

    @Autowired
    BroadcastService broadcastService;

    /**
     * @author zlz
    //	 * @param 通过ID查询设备
     * */
    @RequestMapping(value="/selectEquipmentById")
    @ResponseBody
    public Msg selectEquipmentById(String equipmentId){
        Equipment equipment = equipmentService.selectEquipmentById(equipmentId);
        return Msg.success().add("equipment",equipment);
    }
    /**
     * @author zlz
    //	 * @param 查询所有设备
     * */
    @RequestMapping("/selectEquiment")
    @ResponseBody
   public Msg selectEquipment(@RequestParam(value = "pn", defaultValue="1") Integer pn){
        PageHelper.startPage(pn, 12);
        List<Equipment> list = equipmentService.selectEquipment(pn);
        PageInfo<Equipment> page = new PageInfo<Equipment>(list,  5);
        return Msg.success().add("pageInfo", page);

   }
    /**
     * @author zlz
    //	 * @param 修改
     * */
   @RequestMapping(value ="/updateEquipment")
   @ResponseBody
    public Msg updateEquipment(Equipment equipment){
       int result = equipmentService.updateEquipment(equipment);
        if(result==0){
            return Msg.fail();
        }else {
            return Msg.success();
        }
    }
    /**
     * @author zlz
    //	 * @param 添加设备
     * */
  @RequestMapping("/addEquipment")
  @ResponseBody
  public Msg addEquipment(Equipment equipment){

     int result =  equipmentService.addEquipment(equipment);
      if(result==0){
          return Msg.fail();
      }else{
          return Msg.success();
      }
  }
    /**
     * @author zlz
    //	 * @param 删除设备
     * */
  @RequestMapping(value = "/deleteEquipment")
  @ResponseBody
  public Msg deleteEquipment(String equipmentId ) {

      int result = equipmentService.deleteEquipment(equipmentId);
        if(result==0){
            return Msg.fail();
        }else{
            return Msg.success();
        }
  }
    /**
     * @author zlz
    //	 * @param 通过名字查询设备
     * */
    @RequestMapping("/selectEquipmentByName")
    @ResponseBody
    public Msg selectEquipmentByName(@RequestParam(value = "pn", defaultValue="1") Integer pn,@RequestParam(value = "equipmentName", defaultValue="1")String equipmentName){
        PageHelper.startPage(pn, 5);
        List<Equipment> list = equipmentService.selectEquipmentByName(equipmentName);
        PageInfo<Equipment> page = new PageInfo<Equipment>(list,  5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author zlz
    //	 * @param 通过状态查询设备
     * */
    @RequestMapping("/selectEquipmentByState")
    @ResponseBody
    public Msg selectEquipmentByState(@RequestParam(value = "pn", defaultValue="1") Integer pn,@RequestParam(value = "equipmentState", defaultValue="1")String equipmentState){
        PageHelper.startPage(pn, 5);
        List<Equipment> list = equipmentService.selectEquipmentByState(equipmentState);
        PageInfo<Equipment> page = new PageInfo<Equipment>(list,  5);
        return Msg.success().add("pageInfo", page);
    }

}
